﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using ANGFLib;

namespace ANGF
{
    public partial class FormEditPlace : Form
    {
        private MyXmlDoc doc;
        private XElement targetElement;
        public FormEditPlace(MyXmlDoc doc, XElement targetElement)
        {
            InitializeComponent();
            this.doc = doc;
            this.targetElement = targetElement;
        }

        private void updateIfExist(Action<string> setter, string elementName)
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            XElement element = targetElement.Element(ns + elementName);
            if (element != null) setter(element.Value);
        }

        private void FormEditPlace_Load(object sender, EventArgs e)
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            userControlSuperTalkEditor1.SetMyXmlDoc(doc);
            this.checkBoxSubPlace.Checked = true;
            this.comboBox1.Items.Clear();
            foreach (var n in General.GetCandidatePlaceList(doc, ns + "Place"))
            {
                comboBox1.Items.Add(n);
            }
            if (targetElement == null)
            {
                this.labelID.Text = Guid.NewGuid().ToString();
                if (comboBox1.Items.Count > 0) this.comboBox1.SelectedIndex = 0;
            }
            else
            {
                this.labelID.Text = targetElement.Element(ns + "Id").Value;
                this.textBoxName.Text = targetElement.Element(ns + "Name").Value;
                updateIfExist((x) => { textBoxX.Text = x; }, "PositionX");
                updateIfExist((y) => { textBoxY.Text = y; }, "PositionY");
                if (targetElement.Element(ns + "Parent") != null)
                {
                    for (int i = 0; i < comboBox1.Items.Count; i++)
                    {
                        var p = targetElement.Element(ns + "Parent");
                        if (p != null && ((Place)comboBox1.Items[i]).Id == p.Value)
                        {
                            this.comboBox1.SelectedIndex = i;
                        }
                    }
                    checkBoxSubPlace.Checked = true;
                }
                else
                {
                    if (comboBox1.Items.Count > 0) this.comboBox1.SelectedIndex = 0;
                    checkBoxSubPlace.Checked = false;
                }
                updateIfExist((x) => { userControlSuperTalkEditor1.Text = x; }, "See");
            }
        }

        private void checkBoxSubPlace_CheckedChanged(object sender, EventArgs e)
        {
            this.textBoxX.Enabled = !this.checkBoxSubPlace.Checked;
            this.textBoxY.Enabled = !this.checkBoxSubPlace.Checked;
            this.comboBox1.Enabled = this.checkBoxSubPlace.Checked;
        }

        private string createItemString(int index)
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            if (index < 0) return "";
            Place n = (Place)this.comboBox1.Items[index];
            if (n == null) return "";
            return n.HumanReadableName + " (" + n.Id + ")";
        }

        private void comboBox1_DrawItem(object sender, DrawItemEventArgs e)
        {
            e.DrawBackground();
            string s = createItemString(e.Index);
            e.Graphics.DrawString(s, e.Font, new SolidBrush(e.ForeColor), e.Bounds);
            e.DrawFocusRectangle();
        }
        private bool validateInt(string s, string name)
        {
            int n;
            bool result = int.TryParse(s, out n);
            if (result == false)
            {
                MessageBox.Show(this, name + "は整数でなければなりません", this.Text);
            }
            return result;
        }
        private void buttonOK_Click(object sender, EventArgs e)
        {
            XNamespace ns = ANGFLib.XmlNamespacesConstants.StdXmlNamespace;
            // validation
            if (textBoxName.Text.Trim().Length == 0)
            {
                MessageBox.Show(this, "名前が入力されていません", this.Text);
                return;
            }
            if (!this.checkBoxSubPlace.Checked)
            {
                if (!validateInt(textBoxX.Text, "X")) return;
                if (!validateInt(textBoxY.Text, "Y")) return;
            }

            // add/update
            var elem = targetElement ?? new XElement(ns + "Place");
            elem.RemoveAll();
            elem.Add(new XElement(ns + "Id", labelID.Text));
            elem.Add(new XElement(ns + "Name", textBoxName.Text));
            if (this.checkBoxSubPlace.Checked)
            {
                elem.Add(new XElement(ns + "Parent", ((Place)comboBox1.Items[comboBox1.SelectedIndex]).Id));
            }
            else
            {
                elem.Add(new XElement(ns + "PositionX", textBoxX.Text));
                elem.Add(new XElement(ns + "PositionY", textBoxY.Text));
            }
            var superTalk = this.userControlSuperTalkEditor1.Text;
            if (!string.IsNullOrWhiteSpace(superTalk)) elem.Add(new XElement(ns + "See", new XElement(ns + "SuperTalk", superTalk)));


            if (targetElement == null)
            {
                doc.moduleEx.Add(elem);
            }
            this.Tag = labelID.Text;
            DialogResult = System.Windows.Forms.DialogResult.OK;
            Close();
        }
    }
}
